<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="flex-1" :model="formState" :colon="false" :label-col="{ style: { width: '70px' } }">
        <a-row :gutter="24">
          <a-col :span="8">
            <a-form-item name="dateRange" label="日期范围">
              <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <a-space class="mb-3">
        <BaseButton type="primary" @click="onClickOpen()">核算</BaseButton>
        <!-- <a-button @click="onExport">导出</a-button> -->
      </a-space>
      <BaseTable :loading="loading" column-code="00001165" :data-source="tableData" :pagination="pagination" :scroll="{ x: 1000 }" fill>
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'action'">
            <BaseButton type="link" size="small" @click="onCLickControl(record)">明细</BaseButton>
            <a-divider type="vertical" />
            <BaseButton confirm-text="确认删除？" type="link" danger :disabled="!record.isDel" size="small" @click="onCLickDel(record)">删除</BaseButton>
          </template>
          <!-- :disabled="!record.isDel" -->
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="onSuceess" />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import dayjs from 'dayjs'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import BaseButton from '@/components/BaseButton'
import Edit from './Edit'

const router = useRouter()
const formState = ref({
  dateRange: [dayjs(new Date().getFullYear() + '-01-01').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
})
const editOpen = ref(false)
const rowData = ref(null)
const formRef = ref(null)
const { loading, tableData, getData, pagination } = useTable({
  url: '/drugStorage/comprehenive/getMonthWeightedAverageList',
  immediate: true,
  formatParams: () => {
    return {
      kssj: formState.value.dateRange?.[0],
      jssj: formState.value.dateRange?.[1]
    }
  }
})
const onSuceess = () => {
  getData()
}
const onCLickControl = async (record) => {
  router.push({
    path: '/drug/finance/calculate-detail',
    query: {
      lsh: record.lsh
    }
  })
  getData()
}

const onCLickDel = async (record) => {
  const res = await post('/drugStorage/comprehenive/doDelMonthWeightedAverage', { lsh: record.lsh })
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  getData()
}
const onClickOpen = async () => {
  const { code, data, msg } = await post('/drugStorage/comprehenive/getLastMonth', { bh: 0 })
  if (code !== 0) return message.error(msg || '暂无最新未核算月份')
  editOpen.value = true
  rowData.value = data
}
onMounted(() => {
  getData()
})
</script>

<style lang="less" scoped></style>
